program Summa;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement = ^element;
  element = record
    v1, v2: Integer;
    next: pelement;
  end;

procedure PrintList (head: pelement);
var
  current: pelement;
begin
  current:=head;
  while (current <> nil) do
  begin
    Writeln (current^.v1, ' ', current^.v2);
    current:=current^.next;
  end;
end;


procedure AddToBegin (var head: pelement; num1, num2: integer);
var
  tmp: pelement;
begin
  tmp:=head;
  new(head);
  head^.next:=tmp;
  head^.v1:=num1;
  head^.v2:=num2;
end;

procedure AddToEnd (var head: pelement; num1, num2: integer);
var
  current: pelement;
begin
  if head <> nil then
  begin
    current:=head;
    while (current^.next <> nil) do
    begin
      current:=current^.next;
    end;
    new (current^.next);
    current^.next^.v1:=num1;
    current^.next^.v2:=num2;
    current^.next^.next:=nil;
  end else begin
    AddToBegin(head, num1, num2);
  end;
end;


var
  fin : TextFile;
  flag: Boolean;
  n, m, i, a, b, c, d, e, v1: Integer;
  current1, current2, head1, head2, head3: pelement;

begin
  AssignFile(fin, 'inputsum.txt');
  Reset(fin);
  head1 := nil;
  head2 := nil;
  head3 := nil;
  read(fin, n);
  for i := 1 to n do
  begin
    read (fin, a);
    read (fin, v1);
    AddToEnd(head1, a, v1);
  end;
  read(fin, m);
  for i := 1 to m do
  begin
    read (fin, b);
    read (fin, e);
    AddToEnd(head2, b, e);
  end;
  current1:=head1;
  current2 := head2;
  flag := False;
  while (current1 <> nil) and (current2 <> nil) do
  begin
    if current1^.v2 > current2^.v2 then
    begin
      d := current1^.v2;
      c := current1^.v1;
      current1 := current1^.next;
      flag := True;
    end;
    if flag = False then
    begin
      if current1^.v2 < current2^.v2 then
      begin
        d := current2^.v2;
        c := current2^.v1;
        current2 := current2^.next;
        flag := True;
      end;
    end;
    if flag = False then
    begin
      if current1^.v2 = current2^.v2 then
      begin
        d := current1^.v2;
        c := current1^.v1 + current2^.v1;
        current1 := current1^.next;
        current2 := current2^.next;
      end;
    end;
    AddToEnd(head3, c, d);
    flag := False;
  end;
  if (current1 = nil) and (current2 <> nil) then
  begin
    while (current2 <> nil) do
    begin
      d := current2^.v2;
      c := current2^.v1;
      AddToEnd(head3, c, d);
      current2 := current2^.next;
    end;
  end;
  if (current1 <> nil) and (current2 = nil) then
  begin
    while (current1 <> nil) do
    begin
      d := current1^.v2;
      c := current1^.v1;
      AddToEnd(head3, c, d);
      current1 := current1^.next;
    end;
  end;
  PrintList(head3);
  CloseFile(fin);
  readln;
end.
